﻿using System;
using System.Linq;

using Buddy.BehaviorTree;
using Buddy.Common;
using Buddy.CommonBot;
using Buddy.Swtor;

using WingIt.Dynamics;

using Action = Buddy.BehaviorTree.Action;

namespace WingIt.Routines
{
    public static class CommandoGunnery
    {

        [Class(CharacterClass.Trooper, AdvancedClass.Commando, SkillTreeId.CommandoGunnery)]
        [Behavior(BehaviorType.Pull)]
        public static Composite CommandoGunneryPull()
        {
            return new PrioritySelector(
                // new Decorator(ret => BuddyTor.Me.IsMounted, new PrioritySelector()), --- still pulling if mounted
                // We need to sort our movement and line-of-sight stuff out before anything else.
                Movement.MoveTo(ret => BuddyTor.Me.CurrentTarget.Position, 2.8f),
                Movement.StopInRange(2.8f),
                Spell.BuffSelf("Reserve Powercell", ret => BuddyTor.Me.Level > 41 && AbilityManager.HasAbility("Reserve Powercell")),
                Spell.Cast("Plasma Grenade", ret => BuddyTor.Me.Level > 17 && AbilityManager.HasAbility("Plasma Grenade")),
                Spell.Cast("Sticky Grenade", ret => AbilityManager.HasAbility("Sticky Grenade")),
                Spell.Cast("Hammer Shot", ret => true)
                );

        }

        [Behavior(BehaviorType.Combat)]
        [Class(CharacterClass.Trooper, AdvancedClass.Commando, SkillTreeId.CommandoGunnery)]
        public static Composite CommandoGunneryCombat()
        {
            return new PrioritySelector(
                // We need to sort our movement and line-of-sight stuff out before anything else.
                Movement.StopInRange(2.8f),
                #region Lazyraiderish stuff - need to comment out movment stuff for this to work
                new Decorator(ret => BuddyTor.Me.IsMoving,
                    new PrioritySelector(
                        Spell.BuffSelf("Reactive Shield", ret => BuddyTor.Me.HealthPercent < 40),
                        Spell.Cast("Adrenaline Rush", ret => BuddyTor.Me.HealthPercent <= 60),
                        Spell.Cast("Hammer Shot", ret => BuddyTor.Me.ResourceStat < 9),
                        Spell.Cast("Concussion Charge", ret => BuddyTor.Me.ResourceStat >= 2 && Helpers.Targets.Count(t => t.Distance <= .4f) >= 2),
                        Spell.Cast("Demolition Round", ret => BuddyTor.Me.ResourceStat >= 2),
                        Spell.Cast("High Impact Bolt", ret => true),
                        Spell.Cast("Sticky Grenade", ret => true),
                        Spell.Cast("Hammer Shot", ret => true)
                    )
                ),
            #endregion
                Spell.Cast("Grav Round", ret => BuddyTor.Me.ResourceStat >= 2 && BuddyTor.Me.CurrentTarget.Debuffs.FirstOrDefault(B => B.Name == "Gravity Vortex" && B.Caster.Name == BuddyTor.Me.Name) != null && BuddyTor.Me.CurrentTarget.Debuffs.FirstOrDefault(B => B.Name == "Gravity Vortex" && B.Caster.Name == BuddyTor.Me.Name).TimeLeft.Seconds < 6),
                Spell.WaitForCast(),
                Spell.Cast("Tenacity", ret => BuddyTor.Me.IsStunned),
                Spell.BuffSelf("Reactive Shield", ret => BuddyTor.Me.HealthPercent < 40),
                Spell.Cast("Adrenaline Rush", ret => BuddyTor.Me.HealthPercent <= 60),
                Spell.Cast("Full Auto", ret => BuddyTor.Me.ResourceStat >= 2 && BuddyTor.Me.HasBuff("Curtain of Fire")),
                Spell.Cast("Hammer Shot", ret => BuddyTor.Me.ResourceStat < 9),
                Spell.Cast("Concussion Charge", ret => BuddyTor.Me.ResourceStat >= 2 && Helpers.Targets.Count(t => t.Distance <= .4f) >= 2),
                Spell.Cast("Stockstrike", ret => BuddyTor.Me.ResourceStat >= 2 && BuddyTor.Me.CurrentTarget.Distance <= .8f),
                new Decorator(ret => !AbilityManager.HasAbility("Grav Round"),
                    new PrioritySelector(
                        Spell.Cast("Sticky Grenade", castWhen => Helpers.Targets.Count() >= 3 && BuddyTor.Me.ResourceStat >= 10),
                        Spell.Cast("High Impact Bolt", ret => true),
                        Spell.Cast("Charged Bolts", ret => AbilityManager.CanCast("Charged Bolts", BuddyTor.Me.CurrentTarget)),
                        Spell.Cast("Explosive Round", ret => BuddyTor.Me.ResourceStat >= 10),
                        Spell.Cast("Hammer Shot", ret => true)
                    )
                ),
                Spell.Cast("Demolition Round", ret => BuddyTor.Me.CurrentTarget.Debuffs.FirstOrDefault(B => B.Name == "Gravity Vortex" && B.Caster.Name == BuddyTor.Me.Name) != null && BuddyTor.Me.CurrentTarget.Debuffs.FirstOrDefault(B => B.Name == "Gravity Vortex" && B.Caster.Name == BuddyTor.Me.Name).Stacks > 3 && BuddyTor.Me.ResourceStat >= 2),
                Spell.Cast("High Impact Bolt", ret => BuddyTor.Me.CurrentTarget.Debuffs.FirstOrDefault(B => B.Name == "Gravity Vortex" && B.Caster.Name == BuddyTor.Me.Name) != null && BuddyTor.Me.CurrentTarget.Debuffs.FirstOrDefault(B => B.Name == "Gravity Vortex" && B.Caster.Name == BuddyTor.Me.Name).Stacks > 3 && BuddyTor.Me.ResourceStat >= 2 && BuddyTor.Me.HasBuff("Charged Barrel") && BuddyTor.Me.Buffs.FirstOrDefault(B => B.Name == "Charged Barrel").Stacks > 2),
                Spell.Cast("Recharge Cells", ret => BuddyTor.Me.ResourceStat < 2),
                Spell.Cast("Grav Round", ret => BuddyTor.Me.ResourceStat >= 10),
                Spell.Cast("Hammer Shot", ret => true),
                Movement.MoveTo(ret => BuddyTor.Me.CurrentTarget.Position, 2.8f)
                );
        }

        [Class(CharacterClass.Trooper, AdvancedClass.Commando, SkillTreeId.CommandoGunnery)]
        [Behavior(BehaviorType.OutOfCombat)]
        public static Composite ComandoGunneryOutOfCombat()
        {
            return new PrioritySelector(
                Spell.Cast("Recharge and Reload", ret => BuddyTor.Me.ResourceStat <= 6 || BuddyTor.Me.HealthPercent < 100),
                Spell.BuffSelf("Armor-piercing Cell", ret => AbilityManager.HasAbility("Armor-piercing Cell")),
                Spell.BuffSelf("Plasma Cell", ret => !AbilityManager.HasAbility("Armor-piercing Cell")),
                Spell.WaitForCast()
                );
        }
    }
}
